package com.jiaeda.springsecurity.config;


import com.jiaeda.springsecurity.authentication.CustomAuthenctiationFailureHandler;
import com.jiaeda.springsecurity.authentication.CustomAuthenticationSuccessHandler;
import com.jiaeda.springsecurity.properties.SecurityProperties;
import com.jiaeda.springsecurity.validate.code.ValidateCodeFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
 * Created by chengchaopeng on 2018/7/9.
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private CustomAuthenctiationFailureHandler customAuthenctiationFailureHandler;
    @Autowired
    private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;
    @Autowired
    private ValidateCodeFilter validateCodeFilter;
    @Autowired
    private DataSource dataSource;
    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //弹出式登录窗口
        //http.httpBasic()
        //表单式登录窗口
        http
                //添加验证码过滤，验证码过滤在用户密码验证前面
                .addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .formLogin()
                    //用户登录页面
                    .loginPage("/authentication/require")
                    //用户登录验证
                    .loginProcessingUrl("/user/login")
                    .failureHandler(customAuthenctiationFailureHandler)
                    .successHandler(customAuthenticationSuccessHandler)
                    .and()
                .rememberMe()
                    .tokenValiditySeconds(securityProperties.getValidateCode().getRememberMeSeconds())
                    .tokenRepository(persistentTokenRepository())
                    .userDetailsService(userDetailsService)
                .and()
                .authorizeRequests()
                //放开权限
                .antMatchers("/authentication/require",
                        securityProperties.getBrowser().getLoginPage(),
                        "/validate/code").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf().disable();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
//        tokenRepository.setCreateTableOnStartup(true);
        return tokenRepository;
    }

}
